package org.csu.pet_store_2.Controller;

import jakarta.servlet.http.HttpSession;
import org.csu.pet_store_2.Common.CommonResponse;
import org.csu.pet_store_2.Domain.Account;
import org.csu.pet_store_2.Domain.Signon;
import org.csu.pet_store_2.Service.EmailService;
import org.csu.pet_store_2.Service.Sendmessage;
import org.csu.pet_store_2.persistence.AccountMapper;
import org.csu.pet_store_2.persistence.SignonMapper;
import org.ini4j.Profile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.ArrayList;

@RestController
@Transactional
public class AccountController {
    private String  authcodeForPhone ="";
    private String authcodeFormail="";
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignonMapper signonMapper;
    @Autowired
    private Sendmessage sendmessage;
    @Autowired
    private EmailService emailService;
    @PostMapping("/Account")
    public CommonResponse<Account> loginByAccount(@RequestBody Signon signon, HttpSession session){

         if(signonMapper.isPasswordCorrect(signon.getUsername(),signon.getPassword())){
             Account account=accountMapper.getAccountByUserid(signon.getUsername());
             session.setAttribute("account",account);
             return CommonResponse.creatForSuccess("登录成功",account);
         }
         else{
             return CommonResponse.creatForError("用户名或密码错误");
        }
    }
    @GetMapping("/message")
    public CommonResponse<String> sendmessage(@RequestParam("Phone") String Phone){
          sendmessage.smsMessage(Phone,"3344");
          authcodeForPhone="3344";
          return CommonResponse.creatForSuccessMessage("发送成功");
    }
    @GetMapping ("/Phone")
    public CommonResponse<Account> loginByPhone(@RequestParam("Phone") String phone,
                                                @RequestParam("Authcode") String authcode){
        System.out.println(authcodeForPhone);
        System.out.println(phone+authcode);
        if(authcode.equals(authcodeForPhone)){
            Account account =accountMapper.getAccountByPhone(phone);
            return CommonResponse.creatForSuccess("登录成功",account);
        }
        else{
            return CommonResponse.creatForError("验证码错误");
        }

    }
    @PostMapping("/newAccount")
    public CommonResponse<String> register(@RequestBody Account account,@RequestParam("authcode") String authcode,@RequestParam("password") String password){
        System.out.println(authcode);
          if(authcode.equals(authcodeFormail)){
              Signon signon=new Signon();
              signon.setUsername(account.getUserid());
              signon.setPassword(password);
              accountMapper.setNewAccount(account);
              signonMapper.setNewSignon(signon);
              return CommonResponse.creatForSuccessMessage("注册成功");
          }
          else{

              return CommonResponse.creatForSuccessMessage("验证码错误");
          }

    }
    @GetMapping("isAccountExit")
    public CommonResponse<String> isAccountExit(@RequestParam("account") String account){
          if(!accountMapper.isExistAccount(account)){
              return CommonResponse.creatForSuccess("账号尚未存在");
          }
          else{
              return CommonResponse.creatForSuccess("账号已存在");
          }
    }

    @GetMapping("email")
    public CommonResponse<String> email(@RequestParam("email") String email) throws MessagingException {
             emailService.sentSimpleMail("验证码","4455",email);
             authcodeFormail="4455";
             return CommonResponse.creatForSuccessMessage("发送成功");
    }

    @PostMapping("changeAccount")
    public CommonResponse<String> changeAccount(@RequestBody Account account,@RequestParam("password")String password){
        accountMapper.setAddr1ByUserid(account.getUserid(),account.getAddr1());
        accountMapper.setAddr2ByUserid(account.getUserid(),account.getAddr2());
        accountMapper.setEmailByUserid(account.getUserid(),account.getEmail());
        accountMapper.setStateByUserid(account.getUserid(),account.getState());
        accountMapper.setStatusByUserid(account.getUserid(),account.getStatus());
        accountMapper.setCityByUserid(account.getUserid(),account.getCity());
        accountMapper.setCountryByUserid(account.getUserid(),account.getCountry());
        accountMapper.setPhoneByUserid(account.getUserid(),account.getPhone());
        accountMapper.setFirstnameByUserid(account.getUserid(),account.getFirstname());
        accountMapper.setLastnameByUserid(account.getUserid(),account.getLastname());
        signonMapper.setPasswordByUsername(account.getUserid(),password);
        return CommonResponse.creatForSuccessMessage("修改成功");
    }
    @GetMapping("/account")
    public CommonResponse<ArrayList<Account>> getaccount(HttpSession session){
        if(session.getAttribute("account")!=null){
            Account account =(Account) session.getAttribute("account");
            ArrayList<Account> accounts =new ArrayList<>();
            accounts.add(account);
            System.out.println(account);
            return CommonResponse.creatForSuccess("成功",accounts);
        }
            return CommonResponse.creatForError("错误");

    }
    @GetMapping("/account_edit")
    public CommonResponse<Account> getaccountbyedit(HttpSession session){
        if(session.getAttribute("account")!=null){
            Account account =(Account) session.getAttribute("account");
            return CommonResponse.creatForSuccess("成功",account);
        }
        return CommonResponse.creatForError("错误");

    }
 }
